Skip to content

Conversation

@JustAGuyTryingHisBest
Copy link

Motivation

Taken directly from the related issue - This function improves RFC 92 and multi-derivation package support, by allowing the derivation path to be accessed for any output, whose derivations may be distinct, rather than only exposing a single drvPath.

Context

Resolves #10120

Translates solution from NixOS/nixpkgs#281536 to CppNix. Thank you to @roberth for the initial implementation.


Add 👍 to pull requests you find important.

The Nix maintainer team uses a GitHub project board to schedule and track reviews.

@github-actions github-actions bot added the with-tests Issues related to testing. PRs with tests have some priority label Jan 8, 2026
@roberth
Copy link
Member

roberth commented Jan 12, 2026

This should reject nonsensical inputs, just like my Nixpkgs implementation would.

Overly lenient functions produce garbage inputs to other functions, which then produce garbage errors, and we can't make functions less lenient without breaking user expressions, so this really has to be done with care and a bunch of "adversarial" tests.

@JustAGuyTryingHisBest JustAGuyTryingHisBest force-pushed the justaguytryinghisbest/derivationOfPrimop branch from 0fff477 to 210b7fc Compare January 12, 2026 20:31
@github-actions github-actions bot added new-cli Relating to the "nix" command fetching Networking with the outside (non-Nix) world, input locking c api Nix as a C library with a stable interface labels Jan 12, 2026
@JustAGuyTryingHisBest JustAGuyTryingHisBest force-pushed the justaguytryinghisbest/derivationOfPrimop branch from 210b7fc to 08f9ac6 Compare January 12, 2026 20:31
@roberth
Copy link
Member

roberth commented Jan 13, 2026

Awesome. Could you make sure the returned string has the correct context?
Could you also add tests for "wrong" types?
I think we should also have an unrecoverable error for derivationOf ./..
What happens for a valid-looking store path without string context?
Or for CA placeholder paths? (Although we could just open an issue for the ca-derivations milestone for this one)

@JustAGuyTryingHisBest
Copy link
Author

@roberth I appreciate your patience and working through this change with me!

Awesome. Could you make sure the returned string has the correct context?

Done. Does this seem like the right context?

Could you also add tests for "wrong" types?

Done. Do you see any additional tests that might be beneficial?

I think we should also have an unrecoverable error for derivationOf ./..

isStorePath handles most of the error handling for bad store paths

What happens for a valid-looking store path without string context?

see tests/functional/lang/eval-fail-derivationOf-no-context-store-path.err.exp

nix-repl> builtins.derivationOf "/nix/store/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa-test"
error:
       … while calling the 'derivationOf' builtin
         at «string»:1:1:
            1| builtins.derivationOf "/nix/store/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa-test"
             | ^

       error: '/nix/store/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa-test' has no derivation in its context

Or for CA placeholder paths? (Although we could just open an issue for the ca-derivations milestone for this one)

I would opt to open an issue against the ca-derivation milestone.

Copy link
Member

@roberth roberth left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Almost done :)

system = "x86_64-linux";
};
# Create a string with multiple context items by concatenating paths from different derivations
multipleContexts = "${drv1.outPath} ${drv2.outPath}";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Slightly more interesting test case this way, triggering the multiple contexts error instead, iiuc.

Suggested change
multipleContexts = "${drv1.outPath} ${drv2.outPath}";
multipleContexts = "${drv1.outPath}${drv2.outPath}";

system = "x86_64-linux";
};
in
builtins.derivationOf drv.outPath == drv.drvPath
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Extra test case?
I think this works, through outPath string coercion.

Suggested change
builtins.derivationOf drv.outPath == drv.drvPath
builtins.derivationOf drv == drv.drvPath

It might look wrong on the face of it, because drv feels the same as drvPath, but this is actually good.

I'd much prefer to talk about "packages" despite type = "derivation"; on these things; that's just historical.
derivationOf pkg == pkg.drvPath already looks better, and we might as well name it that way in the test.

Packages more closely reflects how we reason about these things. They're all about the outputs, and the derivation (recipe) is just an implementation detail.
String context and outPath string coercion are designed to facilitate that way of thinking.

I've also yapped about this in #6507, and this builtin helps with that direction, by making drvPath largely unnecessary. (And the equality in this test shows the redundancy!)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense. I appreciate the context. I went ahead and added these changes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

c api Nix as a C library with a stable interface fetching Networking with the outside (non-Nix) world, input locking new-cli Relating to the "nix" command with-tests Issues related to testing. PRs with tests have some priority

Projects

None yet

Development

Successfully merging this pull request may close these issues.

derivationOf primop

3 participants